package com.mbbb.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.mbbb.dao.OrderSettingDao;
import com.mbbb.pojo.OrderSetting;
import com.mbbb.service.OrderSettingService;
import com.mbbb.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService{

    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public void addBatch(List<OrderSetting> dataList) {
        if (dataList!=null && dataList.size()!=0){
            for (OrderSetting orderSetting : dataList) {
                //日期不能重复
                OrderSetting orderSetting1 = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
                if (orderSetting1==null){
                    orderSettingDao.add(orderSetting);
                }else {
                    orderSettingDao.update(orderSetting);
                }

            }
        }
    }

    /*this.leftobj = [
                        { date: 1, number: 120, reservations: 1 },
                        { date: 3, number: 120, reservations: 1 },
                        { date: 4, number: 120, reservations: 120 },
                        { date: 6, number: 120, reservations: 1 },
                        { date: 8, number: 120, reservations: 1 }
    ];*/
    @Override
    public List<Map<String, Integer>> getOrderSettingByMonth(String date) {
        String start = date + "-01";
        //String end = date + "-31"; //不是每个月都有31号
        String end = DateUtils.getLastDayOfMonth(date);

        Map map = new HashMap();
        map.put("start",start);
        map.put("end",end);

        System.out.println("map = " + map);

        List<Map<String, Integer>> resultList = new ArrayList<Map<String, Integer>>();

        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        for (OrderSetting orderSetting : list) {
           Map<String, Integer> stringIntegerMap = new HashMap<>();
           stringIntegerMap.put("date",orderSetting.getOrderDate().getDate());
           stringIntegerMap.put("number",orderSetting.getNumber());
           stringIntegerMap.put("reservations",orderSetting.getReservations());
           resultList.add(stringIntegerMap);
        }
        return resultList;
    }

    @Override
    public void saveOrUpdate(OrderSetting orderSetting) {
        OrderSetting orderSettingdb = orderSettingDao.findByOrderDate(orderSetting.getOrderDate());
        if(orderSettingdb==null){
            orderSettingDao.add(orderSetting);
        }else{
            orderSettingDao.update(orderSetting);
        }
    }
}
